﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
using System.Globalization;

namespace MauThuTabControl
{
    /// <summary>
    /// Interaction logic for DMSanPham.xaml
    /// </summary>
    public partial class DMSanPham : Page
    {
        class SanPhamTreeItem : TreeViewItem
        {
        }

        class NhomSanPhamTreeItem : TreeViewItem
        {
        }
        class SanPhamHSXTreeItem : TreeViewItem
        {
        }

        class HSXTreeItem : TreeViewItem
        {
        }

        ConnectionDB cn = new ConnectionDB();
        public DMSanPham()
        {
           
            InitializeComponent();
            LoadGW();
            httree();
           
            
            //_btsua.Visibility = System.Windows.Visibility.Hidden;
            //_bthuysua.Visibility = System.Windows.Visibility.Hidden;
        }

       

       public void LoadCB() 
        {
            DataTable dt = cn.LoadData("select * from DMHangSX");
            DataTable dt1 = cn.LoadData("select * from DMNhomSP");
            DataTable dt2 = cn.LoadData("select * from DMDonViTinh");


            _cbHSX.DisplayMemberPath = "tenHSX";
            _cbHSX.SelectedValuePath = "maHSX";
            _cbHSX.ItemsSource = dt.DefaultView;
            _cbHSX.SelectedIndex = 0;

            _cbNhomSP.DisplayMemberPath = "tenNhomSP";
            _cbNhomSP.SelectedValuePath = "maNhomSP";
            _cbNhomSP.ItemsSource = dt1.DefaultView;
            _cbNhomSP.SelectedIndex = 0;

            _cbDVT.DisplayMemberPath="tenDVT";
            _cbDVT.SelectedValuePath = "maDVT";
            _cbDVT.ItemsSource = dt2.DefaultView;
            _cbDVT.SelectedIndex = 0;
        }

        void LoadGW() 
        {
            DataTable dt = cn.Loadtree("SELECT     dbo.DMSanPham.maSanPham, dbo.DMSanPham.tenSanPham, dbo.DMSanPham.thongSoKT, dbo.DMSanPham.baoHanh, dbo.DMSanPham.giaBan, " +
                     " dbo.DMNhomSP.tenNhomSP, dbo.DMDonViTinh.tenDVT, dbo.DMHangSX.tenHSX " +
                     " FROM dbo.DMDonViTinh INNER JOIN " +
                     " dbo.DMSanPham ON dbo.DMDonViTinh.maDVT = dbo.DMSanPham.maDVT INNER JOIN " +
                     " dbo.DMHangSX ON dbo.DMSanPham.maHSX = dbo.DMHangSX.maHSX INNER JOIN " +
                     " dbo.DMNhomSP ON dbo.DMSanPham.maNhomSP = dbo.DMNhomSP.maNhomSP");
            _dtgdonvitinh.ItemsSource = dt.DefaultView;
            
        }

        void httree()
        {



            treeView1.Items.Clear();
            TreeViewItem tv = new TreeViewItem();
            tv.Header = "Sản phẩm theo nhóm";
            
            
            treeView1.Items.Add(tv);
                DataTable dt = cn.Loadtree("SELECT * FROM DMNhomSP");
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        NhomSanPhamTreeItem nodecon = new NhomSanPhamTreeItem();
                        nodecon.Header = dt.Rows[i]["tenNhomSP"].ToString();
                        nodecon.Tag = dt.Rows[i]["maNhomSP"].ToString();
                        tv.Items.Add(nodecon);

                        
                        string maNhom = dt.Rows[i]["maNhomSP"].ToString();
                        DataTable dt2 = cn.Loadtree("select * from DMSanPham where maNhomSP='" + maNhom + "'");
                        if (dt2.Rows.Count > 0)
                        {
                            for (int j = 0; j < dt2.Rows.Count; j++)
                            {
                                SanPhamTreeItem nodecon1 = new SanPhamTreeItem();
                                nodecon1.Header = dt2.Rows[j]["tenSanPham"].ToString() + "(" + dt2.Rows[j]["maSanPham"].ToString() + ")";
                                nodecon1.Tag = dt2.Rows[j]["maSanPham"].ToString();
                                nodecon.Items.Add(nodecon1);


                            }
                        }
                                               
                    }
                }

                TreeViewItem HSX = new TreeViewItem();
                HSX.Header = "Sản phẩm theo hảng sản xuất";
               
                treeView1.Items.Add(HSX);
                DataTable dt1 = cn.Loadtree("SELECT * FROM DMHangSX");
                if (dt1.Rows.Count > 0)
                {
                    for (int i = 0; i < dt1.Rows.Count; i++)
                    {
                        HSXTreeItem nodecon = new HSXTreeItem();
                        nodecon.Header = dt1.Rows[i]["tenHSX"].ToString();
                        nodecon.Tag = dt1.Rows[i]["maHSX"].ToString();
                        HSX.Items.Add(nodecon);


                        string maHSX = dt1.Rows[i]["maHSX"].ToString();
                        DataTable dt2 = cn.Loadtree("select * from DMSanPham where maHSX='" + maHSX + "'");
                        if (dt2.Rows.Count > 0)
                        {
                            for (int j = 0; j < dt2.Rows.Count; j++)
                            {
                                SanPhamHSXTreeItem nodecon1 = new SanPhamHSXTreeItem();
                                nodecon1.Header = dt2.Rows[j]["tenSanPham"].ToString() + "(" + dt2.Rows[j]["maSanPham"].ToString() + ")";
                                nodecon1.Tag = dt2.Rows[j]["maSanPham"].ToString();
                                nodecon.Items.Add(nodecon1);


                            }
                        }

                    }
                }

          }

        private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            string maNhomSP = null, maSP = null;
            NhomSanPhamTreeItem nspti = treeView1.SelectedItem as NhomSanPhamTreeItem;
            SanPhamTreeItem spti = treeView1.SelectedItem as SanPhamTreeItem;
            string maHSX = null, maSPHSX = null;
            HSXTreeItem hsxti = treeView1.SelectedItem as HSXTreeItem;
            SanPhamHSXTreeItem sphsxti = treeView1.SelectedItem as SanPhamHSXTreeItem;
            if (nspti != null)
            {
                maNhomSP = nspti.Tag.ToString();
                DataTable dt = cn.LoadData("SELECT     dbo.DMSanPham.maSanPham, dbo.DMSanPham.tenSanPham, dbo.DMSanPham.thongSoKT, dbo.DMSanPham.baoHanh, dbo.DMSanPham.giaBan, "+ 
                      " dbo.DMNhomSP.tenNhomSP, dbo.DMHangSX.tenHSX, dbo.DMDonViTinh.tenDVT "+
                      " FROM dbo.DMDonViTinh INNER JOIN "+
                      " dbo.DMSanPham ON dbo.DMDonViTinh.maDVT = dbo.DMSanPham.maDVT INNER JOIN "+
                      " dbo.DMHangSX ON dbo.DMSanPham.maHSX = dbo.DMHangSX.maHSX INNER JOIN "+
                      " dbo.DMNhomSP ON dbo.DMSanPham.maNhomSP = dbo.DMNhomSP.maNhomSP where dbo.DMSanPham.maNhomSP='" + maNhomSP + "' ");
                _dtgdonvitinh.ItemsSource = dt.DefaultView;
            }
            else if (spti != null)
            {
                maSP = spti.Tag.ToString();
                DataTable dt = cn.LoadData("SELECT     dbo.DMSanPham.maSanPham, dbo.DMSanPham.tenSanPham, dbo.DMSanPham.thongSoKT, dbo.DMSanPham.baoHanh, dbo.DMSanPham.giaBan, " +
                      " dbo.DMNhomSP.tenNhomSP, dbo.DMHangSX.tenHSX, dbo.DMDonViTinh.tenDVT " +
                      " FROM dbo.DMDonViTinh INNER JOIN " +
                      " dbo.DMSanPham ON dbo.DMDonViTinh.maDVT = dbo.DMSanPham.maDVT INNER JOIN " +
                      " dbo.DMHangSX ON dbo.DMSanPham.maHSX = dbo.DMHangSX.maHSX INNER JOIN " +
                      " dbo.DMNhomSP ON dbo.DMSanPham.maNhomSP = dbo.DMNhomSP.maNhomSP where dbo.DMSanPham.maSanPham='" + maSP + "' ");
                _dtgdonvitinh.ItemsSource = dt.DefaultView;
            }

            else
            {
                //HSX
                if (hsxti != null)
                {
                    maHSX = hsxti.Tag.ToString();
                    DataTable dt = cn.LoadData("SELECT     dbo.DMSanPham.maSanPham, dbo.DMSanPham.tenSanPham, dbo.DMSanPham.thongSoKT, dbo.DMSanPham.baoHanh, dbo.DMSanPham.giaBan, " +
                          " dbo.DMNhomSP.tenNhomSP, dbo.DMHangSX.tenHSX, dbo.DMDonViTinh.tenDVT " +
                          " FROM dbo.DMDonViTinh INNER JOIN " +
                          " dbo.DMSanPham ON dbo.DMDonViTinh.maDVT = dbo.DMSanPham.maDVT INNER JOIN " +
                          " dbo.DMHangSX ON dbo.DMSanPham.maHSX = dbo.DMHangSX.maHSX INNER JOIN " +
                          " dbo.DMNhomSP ON dbo.DMSanPham.maNhomSP = dbo.DMNhomSP.maNhomSP where dbo.DMSanPham.maHSX='" + maHSX + "' ");
                    _dtgdonvitinh.ItemsSource = dt.DefaultView;
                }
                else if (sphsxti != null)
                {
                    maSPHSX = sphsxti.Tag.ToString();
                    DataTable dt = cn.LoadData("SELECT     dbo.DMSanPham.maSanPham, dbo.DMSanPham.tenSanPham, dbo.DMSanPham.thongSoKT, dbo.DMSanPham.baoHanh, dbo.DMSanPham.giaBan, " +
                          " dbo.DMNhomSP.tenNhomSP, dbo.DMHangSX.tenHSX, dbo.DMDonViTinh.tenDVT " +
                          " FROM dbo.DMDonViTinh INNER JOIN " +
                          " dbo.DMSanPham ON dbo.DMDonViTinh.maDVT = dbo.DMSanPham.maDVT INNER JOIN " +
                          " dbo.DMHangSX ON dbo.DMSanPham.maHSX = dbo.DMHangSX.maHSX INNER JOIN " +
                          " dbo.DMNhomSP ON dbo.DMSanPham.maNhomSP = dbo.DMNhomSP.maNhomSP where dbo.DMSanPham.maSanPham='" + maSPHSX + "' ");
                    _dtgdonvitinh.ItemsSource = dt.DefaultView;
                }
            }

            //TreeViewItem tvi = treeView1.SelectedItem as TreeViewItem;
            //if (tvi != null)
            //{
            //    ma = tvi.Tag.ToString();
            //}
            
                //DataTable dt = cn.LoadData("SELECT     dbo.DMSanPham.maSanPham, dbo.DMSanPham.tenSanPham, dbo.DMSanPham.thongSoKT, dbo.DMSanPham.baoHanh, dbo.DMSanPham.giaBan, dbo.DMDonViTinh.tenDVT, dbo.DMNhomSP.tenNhomSP FROM dbo.DMSanPham INNER JOIN dbo.DMDonViTinh ON dbo.DMSanPham.maDVT = dbo.DMDonViTinh.maDVT INNER JOIN dbo.DMNhomSP ON dbo.DMSanPham.maNhomSP = dbo.DMNhomSP.maNhomSP where dbo.DMSanPham.maNhomSP='"+ma+"' ");
                //_dtgdonvitinh.ItemsSource = dt.DefaultView;
          
           
           
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {


            LoadCB();
        }

        private void _btthem_Click(object sender, RoutedEventArgs e)
        {
            if (_txtTenSP.Text == "" || _txtGB.Text == "" || _txtBH.Text == "" || _txtTSKT.Text == "" || _cbDVT.Text == "" || _cbHSX.Text == "" || _cbNhomSP.Text == "")
            {
                MessageBox.Show("Hãy nhập đầy đủ thông tin", "Chú ý");
            }
            else
            {
                if (_KiemTraKhiLuu())
                {
                    float GB = float.Parse(_txtGB.Text);
                    cn._ThucThiCauTruyVan("INSERT INTO DMSanPham(maSanPham,tenSanPham,maNhomSP,maDVT,maHSX,thongSoKT,baoHanh,giaBan)VALUES('" + _txtMaSP.Text + "',N'" + _txtTenSP.Text + "',N'" + _cbNhomSP.SelectedValue.ToString() + "',N'" + _cbDVT.SelectedValue.ToString() + "',N'" + _cbHSX.SelectedValue.ToString() + "',N'" + _txtTSKT.Text + "','" + _txtBH.Text + "','" + GB + "')");
                    LoadGW();
                    httree();
                    _bthuy_Click(sender, e);
                    MessageBox.Show("Thêm thành công", "Thông báo");
                }
            }
        }

       

         private bool _KiemTraKhiLuu()
        {
           
            
            //kiểm tra số tiền thu
            try
            {
                decimal _SoTienChi = Convert.ToDecimal(_txtGB.Text);

                
               
            }
            catch
            {
                MessageBox.Show("Giá bán không hợp lệ", "Chú ý");
                return false;
            }
            return true;
        }

         private void _btxoa_Click(object sender, RoutedEventArgs e)
         {
             int row = _dtgdonvitinh.SelectedIndex;
             if (row == -1)
             {
                 MessageBox.Show("Bạn phải chọn một dòng cần xóa!", "Chú Ý!");
             }
             else
             {
                 string ma = ((DataRowView)_dtgdonvitinh.SelectedItem).Row[0].ToString();
                

                 if (MessageBox.Show("Bạn có chắc muốn xóa không?", "Chú ý!", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                 {
                    
                   
                         cn._ThucThiCauTruyVan("DELETE FROM DMSanPham WHERE maSanPham=N'" + ma + "'");
                         LoadGW();
                         httree();
                         
                    
                 }


             }
         }

         private void _bsua_Click(object sender, RoutedEventArgs e)
         {
             int row = _dtgdonvitinh.SelectedIndex;
             if (row == -1)
             {
                 MessageBox.Show("Bạn phải chọn một dòng cần sửa!", "Chú Ý!");
             }
             else
             {
                 _txtMaSP.Text = ((DataRowView)_dtgdonvitinh.SelectedItem).Row["maSanPham"].ToString();
                 _txtTenSP.Text = ((DataRowView)_dtgdonvitinh.SelectedItem).Row["tenSanPham"].ToString();
                 _cbDVT.Text = ((DataRowView)_dtgdonvitinh.SelectedItem).Row["tenDVT"].ToString();
                 _cbHSX.Text = ((DataRowView)_dtgdonvitinh.SelectedItem).Row["tenHSX"].ToString();
                 _cbNhomSP.Text = ((DataRowView)_dtgdonvitinh.SelectedItem).Row["tenNhomSP"].ToString();
                 _txtGB.Text = ((DataRowView)_dtgdonvitinh.SelectedItem).Row["giaBan"].ToString();
                 _txtBH.Text = ((DataRowView)_dtgdonvitinh.SelectedItem).Row["baoHanh"].ToString();
                 _txtTSKT.Text = ((DataRowView)_dtgdonvitinh.SelectedItem).Row["thongSoKT"].ToString();
                 _btthem.Visibility = System.Windows.Visibility.Hidden;
                 _bthuy.Visibility = System.Windows.Visibility.Hidden;
                 _btsua.Visibility = System.Windows.Visibility.Visible;
                 _bthuysua.Visibility = System.Windows.Visibility.Visible;
                 _txtMaSP.IsEnabled = false;
                 expander.Header = "Sửa sản phẩm";
                 expander.IsExpanded = true;
             }
         }

         private void _btsua_Click(object sender, RoutedEventArgs e)
         {
             if (MessageBox.Show("Bạn có chắc muốn sửa không?", "Chú ý!", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
             {
                 float GB = float.Parse(_txtGB.Text);
                 cn._ThucThiCauTruyVan("UPDATE DMSanPham SET tenSanPham=N'" + _txtTenSP.Text + "',maNhomSP=N'" + _cbNhomSP.SelectedValue.ToString() + "',maDVT=N'" + _cbDVT.SelectedValue.ToString() + "',maHSX=N'" + _cbHSX.SelectedValue.ToString() + "',thongSoKT=N'" + _txtTSKT.Text + "',baoHanh='" + _txtBH.Text + "',giaBan='" + GB + "' WHERE maSanPham='" + _txtMaSP.Text + "'");

                     LoadGW();
                     httree();
                     _bthuysua_Click(sender, e);
                     MessageBox.Show("Bạn đã sửa thành công!", "Thông báo!");
                
             }
         }

         private void _bthuysua_Click(object sender, RoutedEventArgs e)
         {
             _bthuy_Click(sender, e);
             expander.IsExpanded = false;
             _dtgdonvitinh.UnselectAll();
         }

         private void _bthuy_Click(object sender, RoutedEventArgs e)
         {
             expander.Header = "Thêm sản phẩm";
             _txtMaSP.IsEnabled = true;
             _txtTenSP.Text = "";
             _txtMaSP.Text = "";
             _txtGB.Text = "";
             _txtBH.Text = "";
             _txtTSKT.Text = "";
             _btsua.Visibility = System.Windows.Visibility.Hidden;
             _bthuysua.Visibility = System.Windows.Visibility.Hidden;
             _btthem.Visibility = System.Windows.Visibility.Visible;
             _bthuy.Visibility = System.Windows.Visibility.Visible;
         }

         private void expander_Expanded(object sender, RoutedEventArgs e)
         {
             
             _dtgdonvitinh.IsEnabled = false;
         }

         private void expander_Collapsed(object sender, RoutedEventArgs e)
         {
             _dtgdonvitinh.IsEnabled = true;
             _dtgdonvitinh.UnselectAll();
         }

         private void _btTimKiem_Click(object sender, RoutedEventArgs e)
         {
             if (_rdMaSP.IsChecked == true)
             {
                 if (_txtTimKiem.Text != "")
                 {
                     DataTable dt = cn.Loadtree("SELECT dbo.DMSanPham.maSanPham, dbo.DMSanPham.tenSanPham, dbo.DMSanPham.thongSoKT, dbo.DMSanPham.baoHanh, dbo.DMSanPham.giaBan, " +
                    " dbo.DMNhomSP.tenNhomSP, dbo.DMDonViTinh.tenDVT, dbo.DMHangSX.tenHSX " +
                    " FROM dbo.DMDonViTinh INNER JOIN " +
                    " dbo.DMSanPham ON dbo.DMDonViTinh.maDVT = dbo.DMSanPham.maDVT INNER JOIN " +
                    " dbo.DMHangSX ON dbo.DMSanPham.maHSX = dbo.DMHangSX.maHSX INNER JOIN " +
                    " dbo.DMNhomSP ON dbo.DMSanPham.maNhomSP = dbo.DMNhomSP.maNhomSP where dbo.DMSanPham.maSanPham='" + _txtTimKiem.Text + "'");
                     _dtgdonvitinh.ItemsSource = dt.DefaultView;
                     _dtgdonvitinh.UnselectAll();

                 }
                 else
                 {
                     MessageBox.Show("Nhập thông tin tìm kiếm", "Chú ý");
                 }
             }
             else 
             {
                 if (_txtTimKiem.Text != "")
                 {
                     DataTable dt = cn.Loadtree("SELECT dbo.DMSanPham.maSanPham, dbo.DMSanPham.tenSanPham, dbo.DMSanPham.thongSoKT, dbo.DMSanPham.baoHanh, dbo.DMSanPham.giaBan, " +
                    " dbo.DMNhomSP.tenNhomSP, dbo.DMDonViTinh.tenDVT, dbo.DMHangSX.tenHSX " +
                    " FROM dbo.DMDonViTinh INNER JOIN " +
                    " dbo.DMSanPham ON dbo.DMDonViTinh.maDVT = dbo.DMSanPham.maDVT INNER JOIN " +
                    " dbo.DMHangSX ON dbo.DMSanPham.maHSX = dbo.DMHangSX.maHSX INNER JOIN " +
                    " dbo.DMNhomSP ON dbo.DMSanPham.maNhomSP = dbo.DMNhomSP.maNhomSP where dbo.DMSanPham.tenSanPham like N'%" + _txtTimKiem.Text + "%'");
                     _dtgdonvitinh.ItemsSource = dt.DefaultView;
                     _dtgdonvitinh.UnselectAll();

                 }
                 else
                 {
                     MessageBox.Show("Nhập thông tin tìm kiếm", "Chú ý");
                 }
             }
         }

        

      
    

      
        

      

       

        
    }
}
